home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / plugin / gamelib / exp_sphere.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-09  |  2.4 KB  |  140 lines

  1. #include "..\..\lib\Fly3D.h"
  2. #include "gamelib.h"
  3.  
  4. void exp_sphere::init()
  5. {
  6.     // num segments
  7.     int s=(int)(M_2Pi*radius/flyengine->geomdetail);
  8.     if (s<2) s=2;
  9.     if (s>32) s=32;
  10.  
  11.     int nu=2*s+1; //num vert in u dir
  12.     int ns=s-2; // num strips 
  13.     
  14.     float du=M_2Pi/(nu-1); // step in u
  15.     float dv=M_Pi/s;
  16.  
  17.     int i,j;
  18.     float u,v;
  19.     vertex *verts;
  20.  
  21.     if (sfmesh==0)
  22.         sfmesh=new stripfan_mesh;
  23.     sfmesh->reset();
  24.  
  25.     // create top fan
  26.     verts=sfmesh->add_stripfan(-nu-1,-1,-1);
  27.  
  28.     // set fan center vertex
  29.     verts->pos.vec(0,0,1);
  30.     verts++;
  31.     
  32.     // set fan vertices
  33.     v=M_Pi2-dv;
  34.     u=0.0f;
  35.     for( i=0;i<nu;i++ )
  36.         {
  37.         verts[i].pos.vec(
  38.             (float)(cos(u)*cos(v)),
  39.             (float)(sin(u)*cos(v)),
  40.             (float)sin(v));
  41.         u+=du;
  42.         }
  43.  
  44.     // cerate bottom fan
  45.     verts=sfmesh->add_stripfan(-nu-1,-1,-1);
  46.     
  47.     // set fan center vertex
  48.     verts->pos.vec(0,0,-1);
  49.     verts++;
  50.  
  51.     // set fan vertices
  52.     u=0.0f;
  53.     v=-M_Pi2+dv;
  54.     for( i=0;i<nu;i++ )
  55.         {
  56.         verts[i].pos.vec(
  57.             (float)(sin(u)*cos(v)),
  58.             (float)(cos(u)*cos(v)),
  59.             (float)sin(v));
  60.         u+=du;
  61.         }
  62.  
  63.     // cerate center strips
  64.     v=M_Pi2-dv;
  65.     for( i=0;i<ns;i++ )
  66.         {
  67.         verts=sfmesh->add_stripfan(nu*2,-1,-1);
  68.         u=0.0f;
  69.         for( j=0;j<nu;j++ )
  70.             {
  71.             verts[2*j].pos.vec(
  72.                 (float)(cos(u)*cos(v)),
  73.                 (float)(sin(u)*cos(v)),
  74.                 (float)sin(v));
  75.  
  76.             verts[2*j+1].pos.vec(
  77.                 (float)(cos(u)*cos(v-dv)),
  78.                 (float)(sin(u)*cos(v-dv)),
  79.                 (float)sin(v-dv));
  80.  
  81.             u+=du;
  82.             }
  83.         v-=dv;
  84.         }
  85. }
  86.  
  87. int exp_sphere::step(int dt)
  88. {
  89.     life-=dt;
  90.     float x;
  91.     if (life<0)
  92.         x=0.0f;
  93.     else x=(float)life/source->life;
  94.     curradius=radius*(1.0f-x);
  95.     return 0;
  96. }
  97.  
  98. void exp_sphere::draw()
  99. {
  100.     glPushMatrix();
  101.     glTranslatef(pos.x,pos.y,pos.z);
  102.     glScalef(curradius,curradius,curradius);
  103.     glColor3fv(&color.x);
  104.  
  105.     glDepthMask(GL_FALSE);
  106.     glBlendFunc(GL_ONE, GL_ONE);
  107.     glDisable(GL_FOG);
  108.  
  109.     sfmesh->draw(1);
  110.  
  111.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  112.     if (flyengine->fog) glEnable(GL_FOG);
  113.     glDepthMask(GL_TRUE);
  114.  
  115.     glPopMatrix();
  116. }
  117.  
  118. int exp_sphere::get_custom_param_desc(int i,param_desc *pd)
  119. {
  120.     if (pd!=0)
  121.     switch(i)
  122.     {
  123.     case 0:
  124.         pd->type='c';
  125.         pd->data=&color;
  126.         strcpy(pd->name,"color");
  127.         break;
  128.     }
  129.     return 1;
  130. }
  131.  
  132. int exp_sphere::message(vector& p,float rad,int msg,int param,void *data)
  133. {
  134.     if (msg==FLYOBJM_CHANGEPARAM)
  135.         if (param>=0 || 
  136.             ((param_desc *)data)->data==&flyengine->geomdetail)
  137.             init();
  138.     return 0;
  139. }
  140.